package com.bear.cmcc.NetTestHTTP.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.util.EncodingUtils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.NetTestCellInfoBean;
import com.bear.cmcc.NetTestHTTP.component.NetTestConnecInfoBean;
import com.bear.cmcc.NetTestHTTP.component.NetTestRoundInfoBean;
import com.bear.cmcc.NetTestHTTP.component.NetTestTimesInfoBean;
import com.bear.cmcc.NetTestHTTP.constant.FtpConstant;
import com.bear.cmcc.NetTestHTTP.constant.NetTestDBConstant;
import com.bear.cmcc.NetTestHTTP.db.NetTestDBControl;

public class NetTestDBUtil {
	static private String TAG  = "NetTestDBUtil";
	static private String NET_TEST_DB_ONE_FG = ",,";
	static private String NET_TEST_DB_ONETime_FG = "##";
	static private String NET_TEST_DB_ONETYPE_FG = "&&";
	/**
     * 导出数据
     * @param context
     */
    public static void exportTestDBInfo(Context context)
    {
    	NetTestDBControl dbControl = new NetTestDBControl(context);
    	ArrayList<NetTestRoundInfoBean> roundinfos = NetTestUtil.getNetTestRoundsInfos(context, false);
    	ArrayList<NetTestTimesInfoBean> timesinfos = dbControl.queryAllTimesInfos();
    	ArrayList<NetTestCellInfoBean> cellinfos = dbControl.queryAllCellInfos();
    	ArrayList<NetTestConnecInfoBean> connectinfos = dbControl.queryAllConnectInfos();
    	String allinfos="";
    	if(roundinfos==null){
    		Toast.makeText(context, R.string.current_nodata, Toast.LENGTH_LONG).show();
    		return;
    	}
    	for(int i=0;i<roundinfos.size();i++)
    	{
    		NetTestRoundInfoBean oneInfo = roundinfos.get(i);
    		String tempinfo = oneInfo.getPID()+NET_TEST_DB_ONE_FG+oneInfo.getStartTime()+NET_TEST_DB_ONE_FG+oneInfo.getEndTime()+NET_TEST_DB_ONE_FG+oneInfo.getAverageSpeed()
    				+NET_TEST_DB_ONE_FG+oneInfo.getMaxSpeed()+NET_TEST_DB_ONE_FG+oneInfo.getMinSpeed()+NET_TEST_DB_ONE_FG+oneInfo.getTestTimes()+NET_TEST_DB_ONE_FG+oneInfo.getIsDelete()+NET_TEST_DB_ONE_FG+oneInfo.getDownServer()
    				+NET_TEST_DB_ONE_FG+oneInfo.getConsumeTime();
    		allinfos += tempinfo;
    		if(i<roundinfos.size()-1)
    			allinfos = allinfos + NET_TEST_DB_ONETime_FG;//每次记录间用##号隔开
    	}
    	allinfos = allinfos + NET_TEST_DB_ONETYPE_FG;//round+time+cell+connect表间信息用&&号隔开
    	for(int i=0;i<timesinfos.size();i++)
    	{
    		NetTestTimesInfoBean oneInfo = timesinfos.get(i);
    		Log.i("GYH", String.valueOf(oneInfo.getRoundID()));
    		String tempinfo = oneInfo.getPID()+NET_TEST_DB_ONE_FG+oneInfo.getDownLoadTime()+NET_TEST_DB_ONE_FG+oneInfo.getFileSize()+NET_TEST_DB_ONE_FG+oneInfo.getAverageSpeed()
    				+NET_TEST_DB_ONE_FG+oneInfo.getMaxSpeed()+NET_TEST_DB_ONE_FG+oneInfo.getMinSpeed()+NET_TEST_DB_ONE_FG+oneInfo.getDownServer()+NET_TEST_DB_ONE_FG+oneInfo.getWaveShakeStrong()+NET_TEST_DB_ONE_FG+oneInfo.getRoundID()
    				+NET_TEST_DB_ONE_FG+oneInfo.getIsDelete()+NET_TEST_DB_ONE_FG+oneInfo.getReplayCode()+NET_TEST_DB_ONE_FG+oneInfo.getErrorReason()+NET_TEST_DB_ONE_FG+oneInfo.getNetType();
    		allinfos += tempinfo;
    		if(i<timesinfos.size()-1)
    			allinfos = allinfos + NET_TEST_DB_ONETime_FG;
    	}
    	allinfos = allinfos + NET_TEST_DB_ONETYPE_FG;
    	for(int i=0;i<cellinfos.size();i++)
    	{
    		NetTestCellInfoBean oneInfo = cellinfos.get(i);
    		String tempinfo = oneInfo.getPID()+NET_TEST_DB_ONE_FG+oneInfo.getAverageSpeed()+NET_TEST_DB_ONE_FG+oneInfo.getMaxSpeed()+NET_TEST_DB_ONE_FG+oneInfo.getMinSpeed()+NET_TEST_DB_ONE_FG+oneInfo.getLac()
    				+NET_TEST_DB_ONE_FG+oneInfo.getCi()+NET_TEST_DB_ONE_FG+oneInfo.getCellName()+NET_TEST_DB_ONE_FG+oneInfo.getMaxNetStrong()+NET_TEST_DB_ONE_FG+oneInfo.getMinSpeed()+NET_TEST_DB_ONE_FG+oneInfo.getTimesID()+NET_TEST_DB_ONE_FG+oneInfo.getIsDelete();
    		allinfos += tempinfo;
    		if(i<cellinfos.size()-1)
    			allinfos = allinfos + NET_TEST_DB_ONETime_FG;
    	}
    	allinfos = allinfos + NET_TEST_DB_ONETYPE_FG;
    	for(int i=0;i<connectinfos.size();i++)
    	{
    		NetTestConnecInfoBean oneInfo = connectinfos.get(i);
    		String tempinfo = oneInfo.getPID()+NET_TEST_DB_ONE_FG+oneInfo.getDropPercent()+NET_TEST_DB_ONE_FG+oneInfo.getConnectPercent()+NET_TEST_DB_ONE_FG+oneInfo.getLac()+NET_TEST_DB_ONE_FG+oneInfo.getCi()+NET_TEST_DB_ONE_FG+oneInfo.getIsDelete();
    		allinfos += tempinfo;
    		if(i<connectinfos.size()-1)
    			allinfos = allinfos + NET_TEST_DB_ONETime_FG;
    	}
//    	Log.i(TAG, TAG + "exportdata is=>"+allinfos);
    	
    	String exportPath = Environment.getExternalStorageDirectory().toString() +"/";
    	File dir = new File(exportPath);
    	File file = new File(exportPath + NetTestDBConstant.EXPORT_DATA_FILE_NAME);
    	FileOutputStream fos = null;
        BufferedOutputStream buff = null;
        try {
            if (!dir.exists()) {
                dir.mkdirs();
            }
            if (file.exists()) {
            	file.delete();
            }
            fos = new FileOutputStream(file, true);
            buff = new BufferedOutputStream(fos);
            buff.write(allinfos.getBytes());
            buff.flush();
            Toast.makeText(context, context.getString(R.string.exportdatasuccess)+context.getString(R.string.exportdatafileaddress)+NetTestDBConstant.EXPORT_DATA_FILE_NAME, Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            Log.d(TAG, TAG + " exportData Exception: " + e.getMessage());
            Toast.makeText(context, context.getString(R.string.exportdatafail)+e.getMessage(), Toast.LENGTH_LONG).show();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (buff != null) {
                    buff.close();
                }
            } catch (IOException e) {
                Log.d(TAG, TAG + " exportData finally close IOException: " + e.getMessage());
            }
        }
    }
    
    
    /**
     * 导入数据
     * @param context
     */
    public static void importTestDBInfo(Context context)
    {
    	NetTestDBControl dbControl = new NetTestDBControl(context);
    	dbControl.reCreateTables();
    	String importPath = Environment.getExternalStorageDirectory().toString() +"/";
    	File file = new File(importPath + NetTestDBConstant.EXPORT_DATA_FILE_NAME);
    	String[] roundInfos = null;
    	String[] timesInfos = null;
    	String[] cellInfos = null;
    	String[] connectInfos = null;
    	if(!file.exists())
    	{
    		Toast.makeText(context, context.getString(R.string.importfilenoexit), Toast.LENGTH_SHORT).show();
    	}
    	else{
    		String result = "";
        	FileInputStream fin = null;
        	try
        	{
        		fin = new FileInputStream(importPath + NetTestDBConstant.EXPORT_DATA_FILE_NAME);
    	    	int length = fin.available();
    	        byte[] buffer = new byte[length];
    	        fin.read(buffer);
    	        result = EncodingUtils.getString(buffer, "UTF-8");
    	        fin.close();
    	        String[] allInfo = JsonUtil.splitString(NET_TEST_DB_ONETYPE_FG, result);
    	        roundInfos = JsonUtil.splitString(NET_TEST_DB_ONETime_FG, allInfo[0]);
    	        timesInfos = JsonUtil.splitString(NET_TEST_DB_ONETime_FG, allInfo[1]);
    	        cellInfos = JsonUtil.splitString(NET_TEST_DB_ONETime_FG, allInfo[2]);
    	        connectInfos = JsonUtil.splitString(NET_TEST_DB_ONETime_FG, allInfo[3]);
        	}catch(Exception ex)
        	{
        		Toast.makeText(context, context.getString(R.string.importfilereaderror), Toast.LENGTH_SHORT).show();
        	}
        	try
        	{
        		for(int i=0;i<roundInfos.length;i++)
        		{
        			try
        			{
	        			String[] infos = JsonUtil.splitString(NET_TEST_DB_ONE_FG, roundInfos[i]);
	        			NetTestRoundInfoBean oneInfo = new NetTestRoundInfoBean();
	        			try
	        			{
	        				oneInfo.setPID(Integer.parseInt(infos[0]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setPID(0);
	        			}
	        			oneInfo.setStartTime(infos[1]);
	        			oneInfo.setEndTime(infos[2]);
	        			try
	        			{
	        				oneInfo.setAverageSpeed(Double.parseDouble(infos[3]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setAverageSpeed(0);
	        			}
	        			try
	        			{
	        				oneInfo.setMaxSpeed(Double.parseDouble(infos[4]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setMaxSpeed(0);
	        			}
	        			try
	        			{
	        				oneInfo.setMinSpeed(Double.parseDouble(infos[5]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setMinSpeed(0);
	        			}
	        			try
	        			{
	        				oneInfo.setTestTimes(Integer.parseInt(infos[6]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setTestTimes(0);
	        			}
	    				oneInfo.setIsDelete(infos[7]);
	    				oneInfo.setDownServer(infos[8]);
	    				oneInfo.setConsumeTime(infos[9]);
	    				dbControl.insertToTestRoundInfo(oneInfo);
        			}catch(Exception e)
        			{
        				continue;
        			}
        		}
        		
        		for(int i=0;i<timesInfos.length;i++)
        		{
        			try
        			{
	        			String[] infos = JsonUtil.splitString(NET_TEST_DB_ONE_FG, timesInfos[i]);
	        			NetTestTimesInfoBean oneInfo = new NetTestTimesInfoBean();
	        			
	        			try
	        			{
	        				oneInfo.setPID(Integer.parseInt(infos[0]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setPID(0);
	        			}
	        			oneInfo.setDownLoadTime(infos[1]);
	        			oneInfo.setFileSize(infos[2]);
	        			try
	        			{
	        				oneInfo.setAverageSpeed(Double.parseDouble(infos[3]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setAverageSpeed(0);
	        			}
	        			try
	        			{
	        				oneInfo.setMaxSpeed(Double.parseDouble(infos[4]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setMaxSpeed(0);
	        			}
	        			try
	        			{
	        				oneInfo.setMinSpeed(Double.parseDouble(infos[5]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setMinSpeed(0);
	        			}
	        			oneInfo.setDownServer(infos[6]);
	        			try {
                            oneInfo.setWaveShakeStrong(Double.valueOf(infos[7]));
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
	        			try
	        			{
	        				oneInfo.setRoundID(Integer.parseInt(infos[8]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setRoundID(0);
	        			}
	    				oneInfo.setIsDelete(infos[9]);
	    				try
	        			{
	    					oneInfo.setReplayCode(Integer.parseInt(infos[10]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setReplayCode(0);
	        			}
	    				oneInfo.setErrorReason(infos[11]);
	    				oneInfo.setNetType(infos[12]);
	    				dbControl.insertToTestTimesInfo(oneInfo);
        			}catch(Exception e)
        			{
        				continue;
        			}
        		}
        		
        		for(int i=0;i<cellInfos.length;i++)
        		{
        			try
        			{
	        			String[] infos = JsonUtil.splitString(NET_TEST_DB_ONE_FG, cellInfos[i]);
	        			NetTestCellInfoBean oneInfo = new NetTestCellInfoBean();
	        			
	        			try
	        			{
	        				oneInfo.setPID(Integer.parseInt(infos[0]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setPID(0);
	        			}
	        			try
	        			{
	        				oneInfo.setAverageSpeed(Double.parseDouble(infos[1]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setAverageSpeed(0);
	        			}
	        			try
	        			{
	        				oneInfo.setMaxSpeed(Double.parseDouble(infos[2]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setMaxSpeed(0);
	        			}
	        			try
	        			{
	        				oneInfo.setMinSpeed(Double.parseDouble(infos[3]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setMinSpeed(0);
	        			}
	        			try
	        			{
	        				oneInfo.setLac(Integer.parseInt(infos[4]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setLac(0);
	        			}
	        			try
	        			{
	        				oneInfo.setCi(Integer.parseInt(infos[5]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setCi(0);
	        			}
	        			oneInfo.setCellName(infos[6]);
	        			try
	        			{
	        				oneInfo.setMaxNetStrong(Integer.parseInt(infos[7]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setMaxNetStrong(0);
	        			}
	        			try
	        			{
	        				oneInfo.setMinNetStrong(Integer.parseInt(infos[8]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setMinNetStrong(0);
	        			}
	    				try
	        			{
	    					oneInfo.setTimesID(Integer.parseInt(infos[9]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setTimesID(0);
	        			}
	    				oneInfo.setIsDelete(infos[10]);
	    				dbControl.insertToTestCellInfo(oneInfo);
        			}catch(Exception e)
        			{
        				continue;
        			}
        		}
        		
        		for(int i=0;i<connectInfos.length;i++)
        		{
        			try
        			{
	        			String[] infos = JsonUtil.splitString(NET_TEST_DB_ONE_FG, connectInfos[i]);
	        			NetTestConnecInfoBean oneInfo = new NetTestConnecInfoBean();
	        			
	        			try
	        			{
	        				oneInfo.setPID(Integer.parseInt(infos[0]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setPID(0);
	        			}
	        			try
	        			{
	        				oneInfo.setDropPercent(Integer.parseInt(infos[1]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setDropPercent(0);
	        			}
	        			try
	        			{
	        				oneInfo.setConnectPercent(Integer.parseInt(infos[2]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setConnectPercent(0);
	        			}
	        			try
	        			{
	        				oneInfo.setLac(Integer.parseInt(infos[3]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setLac(0);
	        			}
	        			try
	        			{
	        				oneInfo.setCi(Integer.parseInt(infos[4]));
	        			}catch(Exception e)
	        			{
	        				oneInfo.setCi(0);
	        			}
	    				oneInfo.setIsDelete(infos[5]);
	    				dbControl.insertToTestConnectInfo(oneInfo);
        			}catch(Exception e)
        			{
        				continue;
        			}
        		}
        		Toast.makeText(context, context.getString(R.string.importdatasuccess), Toast.LENGTH_SHORT).show();
        		MyCommonUtil.SendImportDataBroad(context, true);
        	}
        	catch(Exception ex)
        	{
        		Toast.makeText(context, context.getString(R.string.importdatafail), Toast.LENGTH_SHORT).show();
        		MyCommonUtil.SendImportDataBroad(context, false);

        	}
    	}
    	
    }
}
